Player_r = {}
Player_r.__index = Player_r


local Name = "player_r1"
local PLAYER_r_SPEED = 40
--local frame = 0
static_Player_r = 0
derecha_mov = 1
izquierda_mov = 2
arriva_mov = 3
abajo_mov = 4

function Player_r.create(x, y)

	local self = {}
	setmetatable(self, Player_r)
	self.limitX = x
	self.limitY = y
	self["State"] = static_Player_r
	self.x_fin = math.random()*x
	self.y_fin = math.random()*y
	self.imgMov = img.ssJugMov
	self.imgCol = img.ssJugColision
	self.frame = 0
	self.x = math.random()*x
	self.y = math.random()*y
	
	self.collision_jump = false
	self.pixels_jump = 64
	self.x_fin_jump = 0
	self.y_fin_jump = 0
	-- estados 1 = colision, 2 c_up, 3 c_down, 4 c_izq, 5 c_der
	self.estados = { false, false,false,false,false }


	return self
end

function Player_r:update( dt )

	self:updateFrame(dt)
	self:checkState()


	if self.estados[1]== false then

		PLAYER_r_SPEED = 100
	else

		PLAYER_r_SPEED = 0
	end
	
	--self.estados[1]= false
	
	--	if self.estados[1]== true or self.collision_jump == true then
	--
	--		self:collisionJump(dt)
	--	else
		self:updateMoveX(dt)
		self:updateMoveY(dt)
	--	end	
end

function Player_r:draw()

	--love.graphics.print("Fin !ø", self.x_fin, self.y_fin)
	love.graphics.line( self.x_fin, self.y_fin,  self.x, self.y)
	
	if self.estados[1]== false then
		if self["State"] == static_Player_r then
			love.graphics.draw(self.imgMov, quads.pm_static, self.x, self.y)
		elseif self["State"] == derecha_mov then
			love.graphics.draw(self.imgMov, quads.pm_der[math.floor(self.frame % 4)], self.x, self.y)
		elseif self["State"] == izquierda_mov then
			love.graphics.draw(self.imgMov, quads.pm_izq[math.floor(self.frame % 4)], self.x, self.y)
		elseif self["State"] == arriva_mov then
			love.graphics.draw(self.imgMov, quads.pm_up[math.floor(self.frame % 4)], self.x, self.y)
		elseif self["State"] == abajo_mov then
			love.graphics.draw(self.imgMov, quads.pm_down[math.floor(self.frame % 4)], self.x, self.y)
		end
	end

	if self.estados[2] == true then
		--love.graphics.draw(self.imgCol, quads.pm_down[ 0 ], self.x, self.y)
		if self["State"] == static_Player_r then
			love.graphics.draw(self.imgCol, quads.pm_static, self.x, self.y)
		elseif self["State"] == derecha_mov then
			love.graphics.draw(self.imgCol, quads.pm_der[0], self.x, self.y)
		elseif self["State"] == izquierda_mov then
			love.graphics.draw(self.imgCol, quads.pm_izq[0], self.x, self.y)
		elseif self["State"] == arriva_mov then
			love.graphics.draw(self.imgCol, quads.pm_up[0], self.x, self.y)
		elseif self["State"] == abajo_mov then
			love.graphics.draw(self.imgCol, quads.pm_down[0], self.x, self.y)
		end


	end

	if self.estados[3] == true then
		--love.graphics.draw(self.imgCol, quads.pm_down[ 1 ], self.x, self.y)
		if self["State"] == static_Player_r then
			love.graphics.draw(self.imgCol, quads.pm_static, self.x, self.y)
		elseif self["State"] == derecha_mov then
			love.graphics.draw(self.imgCol, quads.pm_der[1], self.x, self.y)
		elseif self["State"] == izquierda_mov then
			love.graphics.draw(self.imgCol, quads.pm_izq[1], self.x, self.y)
		elseif self["State"] == arriva_mov then
			love.graphics.draw(self.imgCol, quads.pm_up[1], self.x, self.y)
		elseif self["State"] == abajo_mov then
			love.graphics.draw(self.imgCol, quads.pm_down[1], self.x, self.y)
		end


	end

	if self.estados[4] == true then
		--love.graphics.draw(self.imgCol, quads.pm_down[ 2 ], self.x, self.y)
		if self["State"] == static_Player_r then
			love.graphics.draw(self.imgCol, quads.pm_static, self.x, self.y)
		elseif self["State"] == derecha_mov then
			love.graphics.draw(self.imgCol, quads.pm_der[2], self.x, self.y)
		elseif self["State"] == izquierda_mov then
			love.graphics.draw(self.imgCol, quads.pm_izq[2], self.x, self.y)
		elseif self["State"] == arriva_mov then
			love.graphics.draw(self.imgCol, quads.pm_up[2], self.x, self.y)
		elseif self["State"] == abajo_mov then
			love.graphics.draw(self.imgCol, quads.pm_down[2], self.x, self.y)
		end


	end

	if self.estados[5] == true then
		--love.graphics.draw(self.imgCol, quads.pm_down[ 3 ], self.x, self.y)
		if self["State"] == static_Player_r then
			love.graphics.draw(self.imgCol, quads.pm_static, self.x, self.y)
		elseif self["State"] == derecha_mov then
			love.graphics.draw(self.imgCol, quads.pm_der[3], self.x, self.y)
		elseif self["State"] == izquierda_mov then
			love.graphics.draw(self.imgCol, quads.pm_izq[3], self.x, self.y)
		elseif self["State"] == arriva_mov then
			love.graphics.draw(self.imgCol, quads.pm_up[3], self.x, self.y)
		elseif self["State"] == abajo_mov then
			love.graphics.draw(self.imgCol, quads.pm_down[3], self.x, self.y)
		end


	end
		
	

	--love.graphics.draw(img.sscampo, quads.sombra, self.x, self.y + 2)
end

function Player_r:updateFrame( dt )

	self.frame = self.frame + dt*13

	if self.frame > 100 then
		self.frame = 0
	end	
end

function Player_r:updateMoveX( dt )

	if self.x_fin - self.x > 0 then
		if self.x_fin < (self.x + math.abs(self.xdif/self.hipotenuse) *  PLAYER_r_SPEED * dt) then
			self.x = self.x_fin
		else
			self.x = self.x + math.abs(self.xdif/self.hipotenuse) * PLAYER_r_SPEED * dt
		end
	else
		if self.x_fin > (self.x - math.abs(self.xdif/self.hipotenuse) * PLAYER_r_SPEED * dt) then
			self.x = self.x_fin
		else
			self.x = self.x - math.abs(self.xdif/self.hipotenuse) * PLAYER_r_SPEED * dt
		end
	end
end

function Player_r:updateMoveY( dt )

	if self.y_fin - self.y > 0 then

		if self.y_fin < (self.y + math.abs(self.ydif/self.hipotenuse) * PLAYER_r_SPEED * dt) then
			self.y = self.y_fin
		else
			self.y = self.y + math.abs(self.ydif/self.hipotenuse) * PLAYER_r_SPEED * dt
		end
	else
		if self.y_fin > (self.y - math.abs(self.ydif/self.hipotenuse) * PLAYER_r_SPEED * dt) then
			self.y = self.y_fin
		else
			self.y = self.y - math.abs(self.ydif/self.hipotenuse) * PLAYER_r_SPEED * dt
		end
	end
end

function Player_r:checkState()
	if self.x == self.x_fin and self.y == self.y_fin then
		self["State"] = static_Player_r
		--hacer que se quede un tiempo en una posicion
		self.x_fin = math.random()*self.limitX
		self.y_fin = math.random()*self.limitY
	else
		self.xdif = self.x - self.x_fin
		self.ydif = self.y - self.y_fin
		hipot = (self.xdif*self.xdif) + (self.ydif*self.ydif) 
		self.hipotenuse = math.sqrt( hipot )

		if math.abs( self.xdif ) >= math.abs( self.ydif ) then

			if self.xdif < 0 then
				self["State"] = derecha_mov

			else
				self["State"] = izquierda_mov 

			end
		else
			if self.ydif < 0 then
				self["State"] = abajo_mov
			else
				self["State"] = arriva_mov 
			end
		end
	end
end

function Player_r:setCol( mask, col )

	self.estados[mask] = col
end

function Player_r:getPointsColission()

	if self["State"] == abajo_mov or self["State"] == arriva_mov or self["State"] == static_Player_r then

		p1 = self.x + 8
		p2 = self.x + 56
		p3 = self.y 
		p4 = self.y + 64

		 return p1,p2,p3,p4
	end


    
	if self["State"] == derecha_mov or self["State"] == izquierda_mov then
		p1 = self.x + 20
		p2 = self.x +44
		p3 = self.y 
		p4 = self.y + 64
		 return p1,p2,p3,p4
	end

	
	return false,false,false,false
end

function Player_r:getPointsColissionUp()
	if self["State"] == abajo_mov or self["State"] == arriva_mov or self["State"] == static_Player_r then

		p1 = self.x + 14
		p2 = self.x + 50
		p3 = self.y 
		p4 = self.y + 6

		 return p1,p2,p3,p4
	end


    
	if self["State"] == derecha_mov or self["State"] == izquierda_mov then
		p1 = self.x + 20 + 6
		p2 = self.x + 44 - 6
		p3 = self.y 
		p4 = self.y + 6
		 return p1,p2,p3,p4
	end

	return false,false,false,false
end

function Player_r:getPointsColissionDown()

	if self["State"] == abajo_mov or self["State"] == arriva_mov or self["State"] == static_Player_r then

		p1 = self.x + 14
		p2 = self.x + 50
		p3 = self.y + 58
		p4 = self.y + 64

		return p1,p2,p3,p4
	end



	if self["State"] == derecha_mov or self["State"] == izquierda_mov then
		p1 = self.x + 20 + 6
		p2 = self.x + 44 - 6
		p3 = self.y + 58
		p4 = self.y + 64
		 return p1,p2,p3,p4
	end

	return false,false,false,false
end

function Player_r:getPointsColissionLeft()

	if self["State"] == abajo_mov or self["State"] == arriva_mov or self["State"] == static_Player_r then

		p1 = self.x + 8
		p2 = self.x + 8 + 6
		p3 = self.y +6
		p4 = self.y + 64 -6

		 return p1,p2,p3,p4
	end

	if self["State"] == derecha_mov or self["State"] == izquierda_mov then
		p1 = self.x + 20
		p2 = self.x + 20 + 6
		p3 = self.y + 6
		p4 = self.y + 64 - 6
		 return p1,p2,p3,p4
	end

	return false,false,false,false
end

function Player_r:getPointsColissionRight()
	if self["State"] == abajo_mov or self["State"] == arriva_mov or self["State"] == static_Player_r then

		p1 = self.x + 56 - 6
		p2 = self.x + 56
		p3 = self.y + 6
		p4 = self.y + 64 - 6

		 return p1,p2,p3,p4
	end

	if self["State"] == derecha_mov or self["State"] == izquierda_mov then
		p1 = self.x + 44 - 6
		p2 = self.x + 44
		p3 = self.y  + 6 
		p4 = self.y + 64 - 6
		 return p1,p2,p3,p4
	end

	return false,false,false,false
end

function Player_r:getC()

	return self.estados[1]
end

--El muñeco tiene que saltar una distancia de 64 pixels a una altura maxima de 32
function Player_r:collisionJump( dt )

	if self.collision_jump == false and self.estados[1]== true then

		self.collision_jump = true

		if self.collisionPlayer_up == true then

			self.x_fin_jump = math.random( self.x - 128, self.x + 128 )
			self.y_fin_jump = math.random( self.y, self.y + 128 )
		end    		

		if self.collisionPlayer_down == true then

			self.x_fin_jump = math.random( self.x - 128, self.x + 128 )
			self.y_fin_jump = math.random( self.y - 128, self.y )
		end  

		if self.collisionPlayer_left == true then

			self.x_fin_jump = math.random( self.x, self.x + 128 )
			self.y_fin_jump = math.random( self.y- 128, self.y + 128 )
		end  

		if self.collisionPlayer_right == true then

			self.x_fin_jump = math.random( self.x - 128, self.x )
			self.y_fin_jump = math.random( self.y - 128, self.y + 128 )
		end 

		
	end



	if self.x == self.x_fin_jump and self.y == self.y_fin_jump then
		self.collision_jump = false
		self.estados[1]= false
	else

		self.xdif_jump = self.x - self.x_fin_jump
		self.ydif_jump = self.y - self.y_fin_jump
		hipot_jump = (self.xdif_jump*self.xdif_jump) + (self.ydif_jump*self.ydif_jump) 
		self.hipotenuse_jump = math.sqrt( hipot_jump )

	--[[		if math.abs( self.xdif_jump ) >= math.abs( self.ydif_jump ) then

			if self.xdif_jump < 0 then
				self["State"] = derecha_mov

			else
				self["State"] = izquierda_mov 

			end
		else
			if self.ydif_jump < 0 then
				self["State"] = abajo_mov
			else
				self["State"] = arriva_mov 
			end
		end]]


	self:updateMoveX_jump(dt)
	self:updateMoveY_jump(dt)
	end
end

function Player_r:updateMoveX_jump( dt )

	if self.x_fin_jump - self.x > 0 then
		if self.x_fin_jump < (self.x + math.abs(self.xdif_jump/self.hipotenuse_jump) *  300 * dt) then
			self.x = self.x_fin_jump
		else
			self.x = self.x + math.abs(self.xdif_jump/self.hipotenuse_jump) * 300 * dt
		end
	else
		if self.x_fin_jump > (self.x - math.abs(self.xdif_jump/self.hipotenuse_jump) * 300 * dt) then
			self.x = self.x_fin_jump
		else
			self.x = self.x - math.abs(self.xdif_jump/self.hipotenuse_jump) * 300 * dt
		end
	end
end

function Player_r:updateMoveY_jump( dt )

	if self.y_fin_jump - self.y > 0 then

		if self.y_fin_jump < (self.y + math.abs(self.ydif_jump/self.hipotenuse_jump) * 300 * dt) then
			self.y = self.y_fin_jump
		else
			self.y = self.y + math.abs(self.ydif_jump/self.hipotenuse_jump) * 300 * dt
		end
	else
		if self.y_fin_jump > (self.y - math.abs(self.ydif_jump/self.hipotenuse_jump) * 300 * dt) then
			self.y = self.y_fin_jump
		else
			self.y = self.y - math.abs(self.ydif_jump/self.hipotenuse_jump) * 300 * dt
		end
	end
end

--[[
	function Player_r:collisionn( x, y )
		if self["State"] == abajo_mov or self["State"] == arriva_mov then
			--self.estados[1]= ( self.x < (x + 64)  and (self.x + 64) > x and self.y < (y + 64)  and (self.y + 64 )> y ) 

			self.estados[1]= ( self.x + 8 < (x + 56)  and (self.x + 56) > x + 8 and self.y < (y + 64)  and (self.y + 64 )> y ) 

			if self.estados[1]== true then

				self.collisionPlayer_up = ( self.x + 8 < (x + 56)  and (self.x + 56) > x + 8 and self.y < (y + 10)  and (self.y + 10 )> y ) 

				self.collisionPlayer_down = ( self.x + 8 < (x + 56)  and (self.x + 56) > x + 8 and self.y + 54 < (y + 64)  and (self.y + 64 ) > y+54 ) 

				self.collisionPlayer_left = ( self.x + 8 < (x + 18)  and (self.x + 18) > x + 8 and self.y < (y + 64)  and (self.y + 64 )> y ) 

				self.collisionPlayer_right = ( self.x + 46 < (x + 56)  and (self.x + 56) > x + 46 and self.y < (y + 64)  and (self.y + 64 )> y ) 
			end

		end
		if self["State"] == derecha_mov or self["State"] == izquierda_mov then
			self.estados[1]= ( self.x + 20 < (x + 44)  and (self.x + 44) > x + 20 and self.y < (y + 64)  and (self.y + 64 )> y ) 

			if self.estados[1]== true then

				self.collisionPlayer_up    = ( self.x + 20 < (x + 44)  and (self.x + 44) > x + 20 and self.y < (y + 10)  and (self.y + 10 )> y ) 
				self.collisionPlayer_down  = ( self.x + 20 < (x + 44)  and (self.x + 44) > x + 20 and self.y + 56 < (y + 64)  and (self.y + 64 )> y + 56 ) 
				self.collisionPlayer_left  = ( self.x + 20 < (x + 30)  and (self.x + 30) > x + 20 and self.y < (y + 64)  and (self.y + 64 )> y ) 
				self.collisionPlayer_right = ( self.x + 34 < (x + 44)  and (self.x + 44) > x + 34 and self.y < (y + 64)  and (self.y + 64 )> y ) 
			end
		end
	end 

	function Player_r:getx()

		return self.x
	end

	function Player_r:gety()

		return self.y
	end

	function Player_r:collisionMouse( x, y )

		self.collision = ( self.x < x + 10  and self.x + 64 > x and self.y < y + 10  and self.y + 64 > y ) 
	end ]]
